/**
 * Used for writing text to xml files.
 * 
 * @author Jeroen
 */

package com.eDriveV2;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;

import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class XMLWriter {
	private File xmlFile;
	
	public XMLWriter() {
		//source: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
		//Check if external storage is available
		boolean mExternalStorageAvailable = false;
		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
		    mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
		    // We can only read the media
		    mExternalStorageAvailable = true;
		    mExternalStorageWriteable = false;
		} else {
		    // Something else is wrong. It may be one of many other states, but all we need
		    // to know is we can neither read nor write
		    mExternalStorageAvailable = mExternalStorageWriteable = false;
		}
		//end copy-paste
		if (mExternalStorageWriteable) {
			GregorianCalendar time = new GregorianCalendar();
			time.getInstance();
			String date;
			date = time.get(time.DAY_OF_MONTH) + "-" + (time.get(time.MONTH) + 1)+ "-" +(time.get(time.YEAR))+ "_" + (time.get(time.HOUR_OF_DAY))+ "," + (time.get(time.MINUTE));
			
			xmlFile = new File(Environment.getExternalStorageDirectory(), "eDriveData/rides/"+date+".xml");
			if (!xmlFile.exists()) {
				try {
					xmlFile.createNewFile();
				} catch (IOException e) {
					Log.e("FileIO", "Test file could not be created!\n" + e.toString());
				}
			}
		} else {
			
		}
	}
	
	/*
	 * Method to write begin of the xml file. 
	 */
	private void writeBeginFile() {
		try {
			FileWriter fw = new FileWriter(xmlFile);
			fw.write("<?xml version='1.0'?>\n");
			fw.write("<ride>\n");
			fw.close();
		} catch(IOException e) {
			Log.e("FileIO", "Error writing to xml file!");
		}
	}
	
	/*
	 * Method to write end of the xml file. 
	 */
	private void writeEndFile() {
		try {
			FileWriter fw = new FileWriter(xmlFile);
			fw.write("</ride>");
			fw.close();
		} catch(IOException e) {
			Log.e("FileIO", "Error writing to xml file!");
		}
	}
	
	/**
	 * Method to write the given HashMap to an xml file on external storage device.
	 * HashMap MUST contain: "weet nog niet ;)"
	 * 
	 * @param data
	 */
	public void writeToFile(HashMap data) {
		try {
			FileWriter fw = new FileWriter(xmlFile);
			String latitude;
			String longitude;
			String speed;
			String efficiency;
			String efficiencyLabel;
			String time;
			
			String rawData;
			String[] rawDataArray;
			fw.write("<?xml version='1.0'?>\n");
			fw.write("<ride>\n");
			Log.d("HashMap size:", String.valueOf(data.size()));
			for (int i = 0; i < data.size(); i++) {
				//split the string into an string array, so it can be
				//inserted into the HashMap
				//rawData = (String)data.get("t"+i);
				rawData = (String)data.get("t"+i);
				Log.d("TEST", data.keySet().toString());
				rawDataArray = rawData.split(", ");
				Log.d("Array:", rawDataArray.toString());
				Log.d("RawData length:", String.valueOf(rawDataArray.length));
				latitude = rawDataArray[0];
				longitude = rawDataArray[1];
				speed = rawDataArray[2];
				efficiency = rawDataArray[3];
				efficiencyLabel = rawDataArray[4];
				time = rawDataArray[5];
				Log.d("Lat:", String.valueOf(latitude));
				Log.d("Lon:", String.valueOf(longitude));
				Log.d("Speed:", String.valueOf(speed));
				fw.write("<Wpt "+
						 "lat=\""+latitude+"\" "+
						 "long=\""+longitude+"\" "+
						 "speed=\""+speed+"\" "+
						 "eff=\""+efficiency+"\" "+
						 "effL=\""+efficiencyLabel+"\" "+
						 "t=\""+time+"\">"+
						 "\n");
			}
			fw.write("</ride>");
			fw.close();
			Log.d("FileIO", "Done writing to test file!");
		} catch(IOException e) {
			Log.e("FileIO", "Error writing to xml file!" +e.toString());
		}
	}
}